package controller;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.managerplat.model.DishInfo;
import com.managerplat.model.OrderInfo;
import com.managerplat.model.SellerInfo;
import com.managerplat.model.Sellers;
import com.managerplat.model.UserInfo;
import com.managerplat.service.SellersManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;


@Controller
public class SellerController {
	@Autowired(required=true)
	private SellersManager sm;
	public static List<SellerInfo> list =null;
	public static List<DishInfo> list1 =null;
	public static List<Sellers> list_unRig = null;
	public static List<DishInfo> menuList = null;
	@RequestMapping("/sellerRegist")
	@ResponseBody
	public int sellerRegist(@RequestBody String seller_json) throws Exception
	{
//		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(seller_json);
//		Sellers seller = (Sellers) JSONObject.toBean(jsonObject, Sellers.class);
//		

		Gson gson = new Gson();
		Sellers seller = gson.fromJson(seller_json, Sellers.class);
		//System.out.println(seller.getSellerIDCardPath().length());
		byte[] idcard = gson.fromJson(seller.getSellerIDCardPath(), byte[].class);
		String sellerIDCardPath = "E:\\sellerIdCard_image\\"+seller.getSellerName()+(int)(Math.random()*10)+"_ic.jpg";
		FileOutputStream fos = new FileOutputStream(sellerIDCardPath);
		fos.write(idcard);
		fos.flush();
		fos.close();
		seller.setSellerIDCardPath(sellerIDCardPath);
		
		byte[] sellerImage = gson.fromJson(seller.getSellerImagePath(), byte[].class);
		String getSellerImagePath = "E:\\sellerIdCard_image\\"+seller.getSellerName()+(int)(Math.random()*10)+"_ic.jpg";
		fos = new FileOutputStream(getSellerImagePath);
		fos.write(sellerImage);
		fos.flush();
		fos.close();
		seller.setSellerImagePath(getSellerImagePath);
		
		byte[] sellerLicence = gson.fromJson(seller.getSellerLicencePath(), byte[].class);
		String getSellerLicencePath = "E:\\sellerIdCard_image\\"+seller.getSellerName()+(int)(Math.random()*10)+"_ic.jpg";
		fos = new FileOutputStream(getSellerLicencePath);
		fos.write(sellerLicence);
		fos.flush();
		fos.close();
		seller.setSellerLicencePath(getSellerLicencePath);
		
		return sm.sellerRegister(seller);
	}
	@RequestMapping("/getSellers")
	@ResponseBody
	public JSONArray getSellerInfo(@ModelAttribute("sellerInfo") JSONArray sellerArray)
	{
		if(list == null || list.isEmpty())
		{
			list = sm.sendSellerInfoToUser();
		}
		sellerArray = (JSONArray) JSONSerializer.toJSON(list);
		return sellerArray;
	}
	@RequestMapping("/getMenu")
	@ResponseBody
	public JSONArray getMenu(Integer sellerId,@ModelAttribute("sellerInfo") JSONArray dishArray)
	{
		if(list1 == null || list1.isEmpty())
		{
			list1 = sm.sendDishInfoToUser(sellerId);
		}
		dishArray = (JSONArray) JSONSerializer.toJSON(list1);
		return dishArray;
	}
	
	@RequestMapping(value = "/sellerLogin")
	@ResponseBody
	public JSONArray sellerLogin(@RequestBody String seller,@ModelAttribute("orderInfo") JSONArray menuArray) {
		System.out.println("sellerLogin"+seller);
		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(seller);
		Sellers s = (Sellers) JSONObject.toBean(jsonObject, Sellers.class);
		if(menuList == null || menuList.isEmpty())
		{
			menuList = sm.sellerLogin(s.getSellerName(),s.getSellerPassword());
		}
		menuArray = (JSONArray) JSONSerializer.toJSON(menuList);
		return menuArray;
	}
	@RequestMapping(value = "/sellerAddMenu")
	@ResponseBody
	public int sellerAddMenu(@RequestBody String jsonArrStr) 
	{
		JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
		 List<DishInfo> list = new ArrayList();  
		 System.out.println(jsonArr.size());
	     	for (int i = 0; i < jsonArr.size(); i++) 
	     	{  
	            list.add((DishInfo) JSONObject.toBean(jsonArr.getJSONObject(i), DishInfo.class));  
	     	} 
	     
		return 0;
	}
	@RequestMapping("/get_unRig")
	@ResponseBody
	public JSONArray get(@ModelAttribute("sellers") JSONArray unRigArray)
	{
		if(list_unRig == null || list_unRig.isEmpty())
		{
			list_unRig = sm.showSellers(0);
		}
		
		unRigArray = (JSONArray) JSONSerializer.toJSON(list_unRig);
		return unRigArray;
	}
	@RequestMapping("/sellerinfo")
	public String sellerInfo()
	{
		return "sellerinfo";
	}
	@RequestMapping("/confirm")
	public String confirmSellerRegist(int sellerId)
	{
		sm.confirmRegist(sellerId);
		return "sellerinfo";
	}
	
}
